using System;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;

namespace SouthernLand.Common
{
    public class BasePage : Page
    {
        public string ApplicationPath
        {
            get
            {
                string url = Request.ApplicationPath;
                if (url.EndsWith("/"))
                    return url;
                else
                    return url + "/";
            }
        }

        public string FullApplicationPath
        {
            get
            {
                return Request.Url.AbsoluteUri.Replace(
                    Request.Url.PathAndQuery, "") + ApplicationPath;
            }
        }

        protected override void InitializeCulture()
        {
            string selectedLanguage;
            if (Request.QueryString["Lang"] == "en-US")
            {
                Session["lang"] = "en-US";
            }
            if (Request.QueryString["Lang"] == "vi-vn")
            {
                Session["lang"] = "vi-vn";
            }
            selectedLanguage = Session["lang"].ToString();
            UICulture = selectedLanguage;
            Culture = selectedLanguage;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
            base.InitializeCulture();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        public String PhysicalApplicationPath()
        {
            return HttpContext.Current.Request.PhysicalApplicationPath;
        }

        protected void RequestLogin()
        {
            Response.Redirect(FormsAuthentication.LoginUrl +
                              "?ReturnUrl=" + Request.Url.PathAndQuery);
        }

        public string FormatDate(DateTime date)
        {
            return date.ToString("MM/dd/yy");
        }

        public static string FormatDouble(double num)
        {
            return num.ToString("#,#").Replace(",", ".");
        }

        public static int GetIntFromQueryString(string key)
        {
            int returnValue = -1;
            string queryStringValue;
            queryStringValue = HttpContext.Current.Request.QueryString[key];
            if (queryStringValue == null)
                return returnValue;
            if (queryStringValue.IndexOf("#") > 0)
                queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

            returnValue = Convert.ToInt32(queryStringValue);
            return returnValue;
        }

        public static string GetStringFromQueryString(string key)
        {
            string returnValue = string.Empty;
            string queryStringValue;
            queryStringValue = HttpContext.Current.Request.QueryString[key];
            if (queryStringValue == null)
                return returnValue;
            if (queryStringValue.IndexOf("#") > 0)
                queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

            returnValue = Convert.ToString(queryStringValue);
            return returnValue;
        }
    }
}